package com.kaibes.imserver.model.database;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONObject;

import com.kaibes.imserver.model.GetColumnsValues;
import com.kaibes.imserver.model.utils.StaticValuesUtil;

public class KaibesimcenterHelper extends DatabaseHelper {

	// private StringFilterUtil stringFilter;
	private static KaibesimcenterHelper instance = null;

	private KaibesimcenterHelper(String database) {
		super(database);
		// stringFilter = new StringFilterUtil();
	}
	
	//contacts
	public void addContactEach(String username, String contactname){
		addContact(username, contactname);
		addContact(contactname, username);
	}
	
	public int addContact(String username, String contactname){
		return addContact(username, contactname, "");
	}
	
	public int addContact(String username, String contactname, String remark){
		try {
			return insert("contacts", "username,contactname,remark", new String[]{username, contactname, remark});
		} catch (SQLException e) {
			System.err.println("重复添加好友！");
//			e.printStackTrace();
		}
		return 0;
	}
	
	public int removeContact(String username, String contactname){
		return delete("contacts", "username = ? and contactname = ?", new String[]{username,contactname});
	}
	
	//notification
	public int addNotification(String username, String contactname, int action, String message){
		try {
			return insert("notification", "username,contactname,action,message", new Object[]{
					username,contactname,action,message
			});
		} catch (SQLException e) {
			System.err.println("重复添加通知！");
//			e.printStackTrace();
		}
		return 0;
	}
	
	public int removeNotification(int id){
		return delete("notification", "id = ?", new Integer[]{id});
	}
	
	public ResultSet getNotificationByUsername(String username){
		return select("notification", "id,contactname,action,message", "username=?", new Object[]{username});
	}
	
	public ResultSet getNotificationById(int id){
		return select("notification", "action,contactname", "id=?", new Integer[]{id});
	}
	
	public int getNotificationId(String message){
		ResultSet resultSet = select("notification", "id", "message=?", new String[]{message});
		try {
			if (resultSet.next()) {
				return resultSet.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return 0;
	}

	public ResultSet getContactsInformations(String[] name) {

		String in = "";
		for (int i = 0; i < name.length - 2; i++) {
			in += "?,";
		}
		in += "?";
		return leftJoin(
				"userinformation",
				"contacts",
				"a.nickname,a.photo,a.sex,a.birth,a.signature,a.introduction,a.job,a.company,a.school,a.address,a.hometown,a.email,b.contactname,b.remark",
				"a.username = b.contactname", "b.contactname in (" + in
						+ ") and b.username = ?", name);

	}

	public ResultSet getContactsInformations(String nickname, String sex,
			String birth_1, String birth_2) {
		return select(
				"userinformation",
				"username,nickname,photo,sex,birth,signature,introduction,job,company,school,address,hometown,email",
				"nickname regexp ? and sex regexp ? and birth >= ? and birth <= ?",
				new String[] { nickname, sex, birth_1, birth_2 });
	}

	public ResultSet getContactname(String username) {
		return select("contacts", "contactname", "username = ?",
				new String[] { username });
	}

	public ResultSet getContactInformation(String username, String contactname) {
		return select("contacts", "username,contactname,remark",
				"username = ? and contactname = ?", new String[] { username,
						contactname });
	}

	public ResultSet getContacts(String username) {
		return select("contacts", "username,contactname,remark",
				"username = ?", new String[] { username });
	}

	public ResultSet getUserInformationOther(String username) {
		ResultSet result = select(
				"userinformation",
				"username,nickname,photo,sex,birth,signature,introduction,job,company,school,address,hometown,email",
				"username = ?", new String[] { username });

		return result;
	}

	public ResultSet getUserinformation(String username) {
		ResultSet result = select(
				"userinformation",
				"username,nickname,photo,sex,birth,signature,introduction,job,company,school,address,hometown,email",
				"username = ?", new String[] { username });

		if (result == null) {
			insertUserinformation(username, "我还没改名字呢", "photo_default.jpg", "女",
					"2000-01-01", "这个人很懒，连签名都懒得改", "我还没改介绍呢", "我还没改工作呢",
					"我还没改公司呢", "我还没改学校呢", "我还没改地址呢", "我还没改家乡呢",
					"admin@admin.com");

			new File(StaticValuesUtil.ROOTFILE_FILES + File.separator
					+ username + File.separator + StaticValuesUtil.PHOTO)
					.mkdirs();
			new File(StaticValuesUtil.ROOTFILE_CACHE + File.separator
					+ username + File.separator + StaticValuesUtil.PHOTO)
					.mkdirs();

			result = getUserinformation(username);
		}

		return result;
	}
	
	private int updateUserInformation(String columns, String selection,
			String[] values, String[] selectionArgs) {
		return update("userinformation", columns, selection, values,
				selectionArgs);
	}

	private int updateUserInformation(String username, String[] columns,
			String[] values) {
		String columnsNew = "";
		for (int i = 0; i < columns.length - 1; i++) {
			columnsNew += columns[i] + " = ?,";
		}
		columnsNew += columns[values.length - 1] + " = ?";
		return updateUserInformation(columnsNew, "username = ?", values,
				new String[] { username });
	}

	public int updateUserInformation(String username, JSONObject jsonObject) {
		GetColumnsValues getColumnsValues = new GetColumnsValues(jsonObject);
		if (getColumnsValues.size() > 0) {
			return updateUserInformation(username, getColumnsValues.getColumns(),
					getColumnsValues.getValues());
		} else {
			return 0;
		}
	}

	public int updateUserPhoto(String username, String photo) {
		return updateUserInformation("photo = ?", "username = ?",
				new String[] { photo }, new String[] { username });
	}

	public int insertUserinformation(String username, String nickname,
			String photo, String sex, String birth, String signature,
			String introduction, String job, String company, String school,
			String address, String hometown, String email) {

		try {
			return insert(
					"userinformation",
					"username,nickname,photo,sex,birth,signature,introduction,job,company,school,address,hometown,email",
					new String[] { username, nickname, photo, sex, birth,
							signature, introduction, job, company, school, address,
							hometown, email });
		} catch (SQLException e) {
			System.err.println("重复添加用户！");
//			e.printStackTrace();
		}
		return 0;
	}

	public int updateContactRemark(String username, String contactname,
			String remark) {
		return update("contacts", "remark = ?",
				"username = ? and contactname = ?", new String[] { remark },
				new String[] { username, contactname });
	}
	
	public static void Initialize() {
		instance = new KaibesimcenterHelper("kaibesimcenter");
	}
	
	public static KaibesimcenterHelper getInstance(){
		return instance;
	}

	@Override
	protected KaibesimcenterHelper clone() throws CloneNotSupportedException {
		return getInstance();
	}
}
